window.onload = function() {

    var url = '/active/index',
        config = {
            elem: '#test',
            url: url,
            cols: [
                [
                    { field: 'id', width: '10%', title: '编号', align: 'center', sort: true }, { field: 'active_name', width: '20%', title: '活动名称' }, { field: 'active_num', width: '10%', align: 'center', title: '成团人数', sort: true }, {
                        field: 'begin_time',
                        width: '20%',
                        align: 'center',
                        title: '有效期',
                        templet: function(item) {
                            var start_time = timestampToTime(parseInt(item.begin_time));
                            var end_time = timestampToTime(parseInt(item.end_time));
                            return start_time + '  --  ' + end_time;
                        }
                    }, {
                        field: 'status',
                        width: '10%',
                        align: 'center',
                        title: '状态',
                        templet: function(item) {
                            switch (parseInt(item.status)) {
                                case 0:
                                    return '暂停中';
                                case 1:
                                    return '进行中';
                                case 2:
                                    return '过期';
                                case 3:
                                    return '禁用';
                            }
                        }
                    }, { fixed: 'right', width: '30%', title: '操作', align: 'center', toolbar: '#barDemo' } //这里的toolbar值是模板元素的选择器

                ]
            ],
            page: true
        };

    layui.use(['form', 'layer', 'jquery', 'table'], function() {
        var table = layui.table,
            form = layui.form,
            layer = layui.layer,
            $ = layui.jquery;
        table.render(config);
        table.on('tool(barDemo)', function(obj) {
            var data = obj.data;
            var layEvent = obj.event;
            var tr = obj.tr;
            if (layEvent == 'detail') {
                location.href = '/active/active-info?id=' + data.id;
            } else if (layEvent == 'edit') {
                location.href = '/active/update?id=' + data.id;
            } else if (layEvent == 'qrcode') {
                Utils.layer.closeAll();
                Utils.layer.load(1, { 'shade': 0.5 });
                $.ajax({
                    url: '/active/qrcode',
                    data: data,
                    method: 'post',
                    dataType: 'json',
                    success: function(data) {
                        Utils.layer.closeAll();
                        if (data.code == 200) {
                            var img = data.data.cdnUrl;
                            //显示图片
                            Utils.layer.open({
                                "content": [
                                    '<form class="layui-form" action="">',
                                    '<img src=' + img + ' style="width:220px;height:220px;padding:0 70px;"/>',
                                    '</form>'
                                ].join(''),
                                "btn": ['确认'],
                                "area": ["400px", "350px"],
                                "yes": function() {
                                    Utils.layer.closeAll();
                                }
                            });

                        } else {
                            Utils.layer.msg(data.msg);
                        }
                    }
                });
            } else if (layEvent === 'del') {
                Utils.layer.open({
                    id: 1,
                    content: '确认要删除吗？',
                    btn: ['确认', '取消'],
                    btn1: function(index, layero) {
                        $.ajax({
                            type: "POST",
                            url: "/active/del",
                            data: { id: data.id },
                            success: function(data) {
                                if (data.code == 200) {
                                    Utils.layer.msg(data.msg);
                                    setTimeout(function() {
                                        table.render(config);
                                    }, 1000)
                                    Utils.layer.close(1)
                                } else {
                                    Utils.layer.msg(data.msg);
                                }
                            }
                        })
                    },
                    btn2: function(index, layero) {}
                })
            } else if (layEvent === 'stop') {
                Utils.layer.open({
                    id: 2,
                    content: '确认要暂停吗？',
                    btn: ['确认', '取消'],
                    btn1: function(index, layero) {
                        $.ajax({
                            type: "POST",
                            url: "/active/set-status",
                            data: { id: data.id, type: 0 },
                            success: function(data) {
                                if (data.code == 200) {
                                    Utils.layer.msg(data.msg);
                                    setTimeout(function() {
                                        table.render(config);
                                    }, 1000)
                                    Utils.layer.close(1)
                                } else {
                                    Utils.layer.msg(data.msg);
                                }
                            }
                        })
                    },
                    btn2: function(index, layero) {}
                })
            } else if (layEvent === 'start') {
                Utils.layer.open({
                    id: 2,
                    content: '确认要激活吗？',
                    btn: ['确认', '取消'],
                    btn1: function(index, layero) {
                        $.ajax({
                            type: "POST",
                            url: "/active/set-status",
                            data: { id: data.id, type: 1 },
                            success: function(data) {
                                if (data.code == 200) {
                                    Utils.layer.msg(data.msg);
                                    setTimeout(function() {
                                        table.render(config);
                                    }, 1000)
                                    Utils.layer.close(1)
                                } else {
                                    Utils.layer.msg(data.msg);
                                }
                            }
                        })
                    },
                    btn2: function(index, layero) {}
                })
            }

        })






        //搜索
        form.on("submit(serch-form)", function(data) {
            var active_name = $("input[name='active_name']").val();
            var one = $('.oneclass option:selected').val();
            var two = $('.twoclass option:selected').val();
            var url = '/active/index/?active_name=' + active_name;
            config.url = url;
            table.render(config);
            return false;
        });

    });
}